package com.jt.www.common.service.impl;

import com.jt.www.common.service.MessageConsumeService;
import com.jt.www.dao.mapper.messageinfo.MessageInfoEntityMapper;
import com.jt.www.dao.mapper.rabbitmqrecord.RabbitmqRecordEntityMapper;
import com.jt.www.model.entity.MessageInfoEntity;
import com.jt.www.model.entity.RabbitmqRecordEntity;
import com.jt.www.model.vo.RabbitMessageInfoVo;
import com.jt.www.util.DateUtils;
import com.jt.www.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author admin_ltf
 * @create 2019-04-13 16:06
 * 检查mq消息消费的service
 */
@Service
public class MessageConsumeServiceImpl implements MessageConsumeService {

    @Autowired
    RabbitmqRecordEntityMapper rabbitmqRecordEntityMapper;
    @Autowired
    MessageInfoEntityMapper messageInfoEntityMapper;


    public void start() {
        // 查询 rabbit_record记录表  未删除的，发送失败的数据
        RabbitmqRecordEntity rabbitmqRecordEntity = new RabbitmqRecordEntity();
        rabbitmqRecordEntity.setIsSend(false);// 未发送
        rabbitmqRecordEntity.setIsDeleted(false);//未删除
        List<RabbitmqRecordEntity> rabbitmqRecordEntityList = rabbitmqRecordEntityMapper.select(rabbitmqRecordEntity);


        // 查询出来后继续消费  即入库 message_info
        consumeMessage(rabbitmqRecordEntityList);

    }

    @Transactional
    public void consumeMessage(List<RabbitmqRecordEntity> rabbitmqRecordEntityList) {
        if (rabbitmqRecordEntityList != null && rabbitmqRecordEntityList.size() > 0) {
            for (RabbitmqRecordEntity entity : rabbitmqRecordEntityList) {
                // 具体的消费方法
                consumeMethod(entity);
            }

        }
    }

    @Transactional
    public void consumeMethod(RabbitmqRecordEntity entity) {
        RabbitmqRecordEntity rabbitmqRecordEntity = null;
        // 消费之前也要去检验一下  该条(同一个messageID)消息 是否已经被消费过。因为同一条消息可以被多次发送
        rabbitmqRecordEntity = new RabbitmqRecordEntity();
        String messageId = entity.getMessageId();
        rabbitmqRecordEntity.setMessageId(messageId);
        rabbitmqRecordEntity.setIsSend(true);// 已发送
//        rabbitmqRecordEntity.setIsDeleted(false);//
        int i = rabbitmqRecordEntityMapper.selectCount(rabbitmqRecordEntity);
        if (i == 0) {
            // 消费
            byte[] recordContent = entity.getRecordContent();
            String content = new String(recordContent);// 把byte[]的消息内容准换为字符串

            // 将消息转化为类  方便消费的时候 做入库处理 message_info
            RabbitMessageInfoVo rabbitMessageInfoVo = null;
            rabbitMessageInfoVo = JsonUtils.fromJson(content, RabbitMessageInfoVo.class);
            //  入库 message_info
            MessageInfoEntity messageInfoEntity = new MessageInfoEntity();
            String currentTime = DateUtils.getCurrentTime();
            messageInfoEntity.setRecordType(1);//消息记录类型: 1-个人消息
            messageInfoEntity.setTargetUser(rabbitMessageInfoVo.getTargetUser());//目标用户
            messageInfoEntity.setTargetOrganization(rabbitMessageInfoVo.getTargetOrganization());//目标机构
            messageInfoEntity.setTargetArea(rabbitMessageInfoVo.getTargetArea());//目标地区
            messageInfoEntity.setMessageType(rabbitMessageInfoVo.getMessageType());//消息类型
            messageInfoEntity.setMessageTitle(rabbitMessageInfoVo.getMessageTitle());//消息标题
            messageInfoEntity.setMessageContent(rabbitMessageInfoVo.getMessageContent());//消息内容 ???
            messageInfoEntity.setMessageUrl(rabbitMessageInfoVo.getMessageUrl());//消息链接
            messageInfoEntity.setNoticeType(rabbitMessageInfoVo.getNoticeType());//通知方式:0-无需通知；1-邮件推送；2-手机短信通知
            messageInfoEntity.setNoticeState(false);//通知状态:0-未通知；1-已通知
            messageInfoEntity.setComment(rabbitMessageInfoVo.getComment());//备注  备注写什么？
            messageInfoEntity.setIsDeleted(false);// 是否删除:1-是；0-否
            messageInfoEntity.setCreateTime(currentTime);//创建时间
            messageInfoEntity.setUpdateTime(currentTime);// 修改时间
            int msgnum = messageInfoEntityMapper.insert(messageInfoEntity);

            //入库成功 修改状态  失败不修改状态
            // 消息消费完之后， 修改rabbit_record  记录的状态  根据uuid做条件
//            rabbitmqRecordEntityMapper.updateRabbitrecordStateByuuid(entity.getMessageId(),true);
            // 改为不用自己手写sql的
            rabbitmqRecordEntity = new RabbitmqRecordEntity();
            rabbitmqRecordEntity.setId(entity.getId());
            rabbitmqRecordEntity.setIsSend(true);
            rabbitmqRecordEntity.setUpdateTime(new Date());
            rabbitmqRecordEntityMapper.updateByPrimaryKeySelective(rabbitmqRecordEntity);
        }
    }
}
